package com.fingard.dsp.bank.directbank.allinpay02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.ReqQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.RetAtsQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.detail.RetAtsRefundDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

import java.io.File;
import java.util.Calendar;

public class AllInPay02QryRefund extends AllInPay02Base implements IQryRefund {
    @Override
    public RetAtsQryRefund d_qry_refund(ReqQryRefund p_req) throws Exception {
        RetAtsQryRefund retAts = new RetAtsQryRefund(p_req.reqHeader);
        retAts.writeStart();

        String tmpRespCode = "";
        String tmpRespInfo = "";

        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req.getQryStartDate());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req.getQryEndDate());
            tmpCalendarTo.add(Calendar.DAY_OF_MONTH, 1);

            while (tmpCalendarFrom.compareTo(tmpCalendarTo) == -1) {
                String refundFilePath = getRefundFileDirectory() + File.separator + "Refund_Data_" + Format.Date10Format.format(tmpCalendarFrom.getTime()) + ".txt";
                File refundFile = new File(refundFilePath);
                if (refundFile.exists()) {
                    StringBuilder refundData = new StringBuilder();

                    refundData.append("<RET_DETAIL>");
                    refundData.append(FileHelper.readFileText(refundFilePath, getCharset()));
                    refundData.append("</RET_DETAIL>");

                    String tmpStrRet = refundData.toString();
                    WriteBankLogStep3(tmpStrRet);

                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet, tmpSbMsg);
                    if (tmpBkReader != null) {
                        tmpRespCode = "0";

                        int totleNum = 0;
                        double totleAmount = 0;

                        while (tmpBkReader.read("/RET_DETAIL/Refund")) {
                            RetAtsRefundDetail tmpRetDetail = new RetAtsRefundDetail();

                            tmpRetDetail.reqBatchNO = tmpBkReader.readedXElement.getSingleText("BATCHID");
                            tmpRetDetail.askNum = tmpBkReader.readedXElement.getSingleText("USERCODE");
                            if (StringHelper.isNullOrEmpty(tmpRetDetail.askNum)) {
                                tmpRetDetail.askNum = tmpBkReader.readedXElement.getSingleText("BATCHID");
                            }
                            tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("ACCOUNT_NO");

                            String amount = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("AMOUNT"));
                            tmpRetDetail.amount = amount;
                            tmpRetDetail.submitTime = Format.DateTimeFormat.format(Format.DateTime14Format.parse(tmpBkReader.readedXElement.getSingleText("SUBMITTIME")));
                            tmpRetDetail.refundTicketDate = Format.DateTimeFormat.format(Format.Date8Format.parse(tmpBkReader.readedXElement.getSingleText("SETTDAY")));
                            tmpRetDetail.refundTicketReason = "退票";

                            totleNum++;
                            totleAmount += Double.parseDouble(amount);

                            retAts.writeRetDetail(tmpRetDetail);
                        }

                        retAts.totleNum = String.valueOf(totleNum);
                        retAts.totleAmount = String.valueOf(totleAmount);
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                    }
                }

                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsQryRefund(p_req.reqHeader);
            retAts.writeStart();
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("所查时间范围内无退票通知");
        }

        retAts.writeEnd();
        return retAts;
    }
}
